%include "Q:\PU2\Macros\hashmerge.sas";

libname input "Q:\External Research Data\AutoPay";
libname output "Q:\PU2\New folder";
libname temp "Q:\PU2\temp";
options threads cpucount=actual;


*This program loops over all months from 2011 to 2020, keeping a list of firms across all months;
%macro loop_clean(y=,m=,start=,end=);
%do i=&start. %to &end.;
%let _timer_start=%sysfunc(datetime());
	%let a=%eval(&m+&i-1);
	%let b=%sysfunc(putn(%eval(&y+%sysevalf(&a/12,floor)),z2.));
	%let c=%sysfunc(putn(%sysevalf(1+&a-12*%sysevalf(&a/12,floor)),z2.));

	*Keep only unique firms in each month;
	proc summary data=input.ap_20&b.&c.(keep=client_code) nway;
		class client_code;
		output out=temp(drop=_type_ _freq_);	
	run;

	*Append;
	proc append base=firm_list data=temp force; run;

	*Delete the temporary files;
	proc datasets nolist;
		delete temp;
	run;

	*End timer;
	data _null_;
		dur=datetime()-&_timer_start;
		put 30*'-' / ' TOTAL DURATION:' dur time13.2 / 30*'-';
	run;
%end;

*Keep only firms across all months;
	proc summary data=firm_list nway;
		class client_code;
		output out=firm_list(drop=_type_ _freq_);	
	run;
	proc sort data=firm_list out=firm_list nodupkey;
		by client_code;
	run;
%mend;

*Create base file;
data firm_list;
	length client_code $ 14;
run;
*%loop_clean(y=11,m=01,start=0,end=108);
%loop_clean(y=08,m=05,start=0,end=140);
/*
data output.firm_list;
	set firm_list;
run;
*/
data output.client_crosswalk;
	set firm_list;
	by client_code;
	retain client_id 0;
	if first.client_code then client_id=client_id+1;
	length client_id 4;
run;
